package com.cskaoyan.controller;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.User;
import com.cskaoyan.bean.validation.ValidationUser;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.service.UserService;
import com.cskaoyan.util.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author stone
 * @date 2022/06/25 11:05
 */
@RestController
@RequestMapping("admin/user")
public class AdminUserController {

    @Autowired
    UserService userService;

    /*@RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit, String sort,String order) {

    }*/

    // localhost:8083/admin/user/list?username=songge&password=niupi&age=25
    @RequestMapping("list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    //public BaseRespVo list(String username, String password, BasePageInfo info) {
    /*public BaseRespVo list( @Valid ValidationUser validationUser,BindingResult bindingResult, BasePageInfo info) {
        if (bindingResult.hasFieldErrors()) {//是否有校验错误
            FieldError fieldError = bindingResult.getFieldError();
            Object rejectedValue = fieldError.getRejectedValue();
            String field = fieldError.getField();
            String defaultMessage = fieldError.getDefaultMessage();
            String msg = "请求参数" + field + "因为值" + rejectedValue + "没有通过校验：" + defaultMessage;
            return BaseRespVo.invalidParameter(msg);
        }
        CommonData<User> data =  userService.query(validationUser.getUsername(), info);
        return BaseRespVo.ok(data);
    }*/
    /*public BaseRespVo list( @Valid ValidationUser validationUser,BindingResult bindingResult, BasePageInfo info) {
        BaseRespVo msg = ValidationUtils.validResult(bindingResult);
        if (msg != null) return msg;
        CommonData<User> data =  userService.query(validationUser.getUsername(), info);
        return BaseRespVo.ok(data);
    }*/

    public BaseRespVo list( @Valid ValidationUser validationUser,BindingResult bindingResult, BasePageInfo info) throws ValidationException {
        // 最终的需求是bindResult.hasFieldErrors方法的返回值为true的情况下响应Json
        ValidationUtils.validResult2(bindingResult);
        // 改造思路 → AspectJ → before或Around通知 → JoinPoint  getArgs

        CommonData<User> data =  userService.query(validationUser.getUsername(), info);
        return BaseRespVo.ok(data);
    }

}
